<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2016/12/1
 * Time: 上午9:09
 */
class Login extends MY_Controller
{
    /**
     * 登录
     * @return [type] [description]
     */
    public function index(){
        $has_error = 0;
        $post_data = $this->input->post();
        if($post_data){
//            print_r($post_data);
            if(empty($post_data['phone'])){
                $this->smarty->assign("phone_err", "请填写手机号");
                $has_error = 1;
            }else{
                $this->smarty->assign("phone", $post_data['phone']);
            }
            if(empty($post_data['pwd'])){
                $this->smarty->assign("pwd_err", "请填写密码");
                $has_error = 1;
            }
            if($has_error==0){
                //判断手机号是否存在
                $this->load->model('User_model');
                $user = $this->User_model->get_user_by_phone($post_data['phone']);

                if(empty($user)){
                    $this->smarty->assign("phone_err", "手机号不存在");
                    $this->smarty->assign("err_msg", "手机号不存在");
                    $has_error = 1;
                }else if($user['is_teacher']==1){
                    $this->smarty->assign("phone_err", "手机号不存在");
                    $this->smarty->assign("err_msg", "手机号不存在");
                    $has_error = 1;
                }else{
                    $password = md5(md5($post_data['pwd']) . $user['rand_str']);

                    if($user['password']!=$password){
                        $this->smarty->assign("pwd_err", "密码错误");
                        $this->smarty->assign("err_msg", "密码错误");
                        $has_error = 1;
                    }
                }



                if($has_error == 0){
                    //设置cookie
                    $user_data = array(
                        'id' => $user['id'],
                        'head_image' => $user['head_image_url'],
                        'user_name' => $user['name'],
                        'mobile' => $user['mobile'],
                        'logged_in' => 1);
                    $this->session->set_userdata('user_data', $user_data);
                    $shichao_data = "uid={$user['id']}&nick={$user['nick']}&head_image_url={$user['head_image_url']}&phone={$user['mobile']}";
                    setcookie('daxuser', $shichao_data, time()+3600*24, '/', '520dax.com');
                    $curr_url =  $this->session->userdata('curr_url');
                    // login_wecenter($post_data['phone'], $post_data['pwd']);
                    if(empty($curr_url) || strstr($curr_url, '/forget')){
                        // header("Location:/user/home");exit;
                        echo "<script> alert('登录成功');parent.location.href='/user/home';  </script>";
                    }else{
                        // header("Location:{$curr_url}");exit;
                        echo "<script> alert('登录成功');parent.location.href='{$curr_url}';  </script>";
                    }
                }
            }

        }
        $this->smarty->display("login.html");
    }

    /**
     * 退出
     * @return [type] [description]
     */
    function logout(){
        $this->session->sess_destroy();
        unset(
            $_SESSION['id'],
            $_SESSION['head_image'],
            $_SESSION['user_name'],
            $_SESSION['mobile'],
            $_SESSION['logged_in']
        );
        $curr_url =  $this->session->userdata('curr_url');
        setcookie('daxuser', "", time() - 1, '/', '520dax.com');
        if(empty($curr_url)){
          header("Location:/");exit;
        }else{
          header("Location:{$curr_url}");exit;
        }
    }


    /**
     * 登录iframe
     * @return [type] [description]
     */
    public function iframe(){
        $post_data = $this->input->post();

        if($post_data){

          //  print_r($post_data);exit;
            if(empty($post_data['phone'])){
                print_r('{"result":"-1","msg":"手机号不能为空"}');exit;
            }
            if(empty($post_data['pwd'])){
                print_r('{"result":"-1","msg":"密码不能为空"}');exit;
            }

            //判断手机号是否存在
            $this->load->model('User_model');
            $user = $this->User_model->get_user_by_phone($post_data['phone']);

            if(empty($user)){
                print_r('{"result":"404","msg":"手机号不存在"}');exit;
            }else{
              $password = md5(md5($post_data['pwd']) . $user['rand_str']);

                if($user['password']!=$password){
                    print_r('{"result":"400","msg":"密码错误"}');exit;
                }else{
                    //设置cookie
                    $user_data = array(
                        'id' => $user['id'],
                        'head_image' => $user['head_image_url'],
                        'user_name' => $user['login_name'],
                        'mobile' => $user['mobile'],
                        'logged_in' => 1);
                    $this->session->set_userdata('user_data', $user_data);
                    print_r('{"result":"0","msg":"success"}');exit;
                  

                }
            }


        }
        $this->smarty->display("login_iframe.html");
    }


}
